package com.vdg.dialog;

import com.vdg.R;

import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow.OnDismissListener;



public class QuickDialog extends PopupWindows  implements OnDismissListener {
	protected View mRootView;
	protected LayoutInflater mInflater;
	private OnDismissListener mDismissListener;

	private boolean mDidAction;
	private int rootWidth = 0;

	
	public View getRootView() {
		return this.mRootView;
	}

	public QuickDialog(Context context, int resourceId) {
		super(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setRootViewId(resourceId);
		
	}
	public QuickDialog(Context context) {
		super(context);
	}
	public void setRootViewId(int id) {
		mRootView = mInflater.inflate(id, null);

		mRootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		setContentView(mRootView);
	}
	public void setContentView( ViewGroup view)
	{
		mRootView=view;
		mRootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		setContentView(mRootView);
	}
	public void clear() {
		mRootView = null;
		System.gc();
		
	}
	public void showAtView(View anchor) {
		preShow();

		int xPos, yPos;

		mDidAction = false;

		int[] location = new int[2];

		anchor.getLocationOnScreen(location);

		Rect anchorRect = new Rect(location[0], location[1], location[0]
				+ anchor.getWidth(), location[1] + anchor.getHeight());
		mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		if (rootWidth == 0) {
			rootWidth = mRootView.getMeasuredWidth();
		}

		int screenWidth = mWindowManager.getDefaultDisplay().getWidth();

		if ((anchorRect.left + rootWidth) > screenWidth) {
			xPos = anchorRect.left - (rootWidth - anchor.getWidth());
			xPos = (xPos < 0) ? 0 : xPos;

		} else {
			if (anchor.getWidth() > rootWidth) {
				xPos = anchorRect.centerX() - (rootWidth / 2);
			} else {
				xPos = anchorRect.left;
			}

		}
		yPos = anchorRect.bottom;

		setAnimationStyle(true);
		mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
	}
	public void showAtView(View anchor,View parent) {
		preShow();

		int xPos, yPos;

		mDidAction = false;

		int[] location = new int[2];

		anchor.getLocationOnScreen(location);

		Rect anchorRect = new Rect(location[0], location[1], location[0]
				+ anchor.getWidth(), location[1] + anchor.getHeight());
		mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		if (rootWidth == 0) {
			rootWidth = mRootView.getMeasuredWidth();
		}

		int screenWidth = mWindowManager.getDefaultDisplay().getWidth();

		if ((anchorRect.left + rootWidth) > screenWidth) {
			xPos = anchorRect.left - (rootWidth - anchor.getWidth());
			xPos = (xPos < 0) ? 0 : xPos;

		} else {
			if (anchor.getWidth() > rootWidth) {
				xPos = anchorRect.centerX() - (rootWidth / 2);
			} else {
				xPos = anchorRect.left;
			}

		}
		yPos = anchorRect.bottom;

		setAnimationStyle(true);
		mWindow.showAtLocation(parent, Gravity.NO_GRAVITY, xPos, yPos);
	}

	public void showAtCenter(View v) {
		preShow();
		int width = mWindowManager.getDefaultDisplay().getWidth();
		int height = mWindowManager.getDefaultDisplay().getHeight();
		mRootView.measure(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		int xPos, yPos;
		xPos = width / 2 - mRootView.getMeasuredWidth() / 2;
		yPos = height / 2 - mRootView.getMeasuredHeight() / 2;
		setAnimationStyle(true);
		mWindow.showAtLocation(v, Gravity.CENTER, 0,0); 
	}
	public void showAtMagirnCenter(View v,int magirnCenter) {
		preShow();
		int width = mWindowManager.getDefaultDisplay().getWidth();
		int height = mWindowManager.getDefaultDisplay().getHeight();
		mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int xPos, yPos;
		xPos = width / 2 - mRootView.getMeasuredWidth() / 2;
		yPos = height / 2 - mRootView.getMeasuredHeight() / 2- magirnCenter;
		setAnimationStyle(true);
		mWindow.showAtLocation(v, Gravity.NO_GRAVITY, xPos, yPos);
	}
	public void showAtBottomCenterHozironal(View v) {
		preShow();
		int width = mWindowManager.getDefaultDisplay().getWidth();
		int height = mWindowManager.getDefaultDisplay().getHeight();
		mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int xPos, yPos;
		xPos = width / 2 - mRootView.getMeasuredWidth() / 2;
		yPos = height ;
		setAnimationStyle(true);
		mWindow.showAtLocation(v, Gravity.NO_GRAVITY, xPos, yPos);
	}
	public void showAtPosition(View v, int posX, int posY) {
		preShow();
		setAnimationStyle(true);
		mWindow.showAtLocation(v, Gravity.NO_GRAVITY, posX, posY);
	}


	private void setAnimationStyle(boolean onTop) {
		mWindow.setAnimationStyle((onTop) ? R.style.Animations_PopUpMenu_Center
				: R.style.Animations_PopDownMenu_Center);

	}

	
	
	public interface OnActionItemClickListener {
		public abstract void onItemClick(QuickDialog source, int pos,
				int actionId);
	}



	@Override
	public void onDismiss() {
		// TODO Auto-generated method stub
		
	}
}